import { addCart } from 'api'
import { getToken } from 'utils'
const mixin = {
  data () {
    return {
      items: []
    }
  },
  methods: {
    addCart ({ id }) {
      if (!getToken()) {
        this.$toast.fail({
          message: '请先登录',
          duration: 1000,
          onClose: () => {
            this.$router.push({
              path: '/login',
              query: {
                from: this.$route.path
              }
            })
          }
        })
        return false
      }
      // 登录了，就加入购物车
      addCart({
        goodsId: id,
        number: 1,
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          this.$toast.success({
            message: '加入成功'
          })
          this.$store.commit('cart/set_items', res.data.data.items)
        }
      })
    },
    onLoad () {
      this.fetchItems()
      this.page++
    },
    onRefresh () {
      this.page = 1
      this.items = []
      this.finished = false
      this.onLoad()
    },
    // 点击进入详情页
    ItemsDetail ({ id }) {
      this.$router.push({
        path: '/detail',
        query: {
          id
        }
      })
    }
  }
}

export default mixin
